#!/bin/bash
cctimestamp=$1
PROJECTNAME=$2
LOG_PATH="/opt/CI/master/userData/groups/group01/projects/$PROJECTNAME/logs"
#LOG_PATH=/opt/CI/master/userData/groups/group01/projects/$PROJECTNAME/logs/log20111114214100*/TQE

cd $LOG_PATH
FLAG=0
COUNT=0
TIME=0
while [ $COUNT -lt 5 ] && [ $TIME -lt 1500 ]
do
cd $LOG_PATH
CUR_LOG_PATH=`find . -name log$cctimestamp -type d | grep -v "temp"`
echo log$cctimestamp
echo "++++++++++++++++++${CUR_LOG_PATH}+++++++++++++++++"
cd $CUR_LOG_PATH/TQE

MESSAGE_TYPE=`cat TQE.xml | grep "message" | sed 's/.*type="//g' | sed 's/".*//g'`
if [ ! -z $MESSAGE_TYPE ];then
case $MESSAGE_TYPE in
        "Info")
        cat TQE.xml | grep "message"
        cat TQE.xml | grep "detail"  
        echo "------------" 
        ;;
        "exception")
        cat TQE.xml | grep "message" 
        cat TQE.xml | grep "detail"  
        echo "------------" 
        ;;
        "cancelled")
        cat TQE.xml | grep "message" 
        cat TQE.xml | grep "detail"  
        echo "------------" 
        echo "cancelled the project~"
        exit 1;
        ;;
        "warning")
        cat TQE.xml | grep "message" 
        cat TQE.xml | grep "detail"  
        echo "------------" 
        echo "warning"  
        FLAG=1
        COUNT=$((COUNT+1))
        ;;
        "error")
        cat TQE.xml | grep "message"
        cat TQE.xml | grep "detail"  
        echo "------------" 
        echo "have some error" 
        FLAG=1
        COUNT=$((COUNT+1))
        ;;
        *)
        cat TQE.xml | grep "message" 
        cat TQE.xml | grep "detail"  
        echo "------------"
        ;;
        esac
fi

###
ERROR_NUM=`cat TQE.xml | grep "module" | sed 's/.*error="//g' | sed 's/".*//g'`
if [ ! -z $ERROR_NUM ];then
if [ $ERROR_NUM == 0 ];then
        echo "success" 
        echo "------------" 
        echo "COUNT= $COUNT" 
        echo "TIME=  $TIME"  
        exit 0;
else
        cat TQE.xml | grep "module" 
        echo "have $ERROR_NUM errors" 
        echo "------------" 
        echo "COUNT= $COUNT" 
        echo "TIME=  $TIME"  
        exit 1;
fi
fi

sleep 15
TIME=$((TIME+15))

done
echo "have 10 errors" 
exit 